<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/25
 * Time: 15:18
 */
namespace app\admin\controller;
use app\admin\model\UpFile;
use think\Controller;
use app\admin\model\System;
use think\Request;

class Systems extends Controller
{
    public function show_list(Request $request){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('systems/del')
            ->addTableHeaderAdd('systems/show_add')
            ->addTableHeaderText('可通过getSysTem(英文名)')
            ->addTableHeaderSousuo()
            ->addTableColumn('id','text','ID')
            ->addTableColumn('name','text','配置名称')
            ->addTableColumn('en_name','text','配置英文名称')
            ->addTableColumn('value','text','配置值')
            ->addTableColumn('tupian','img','图片')
            ->addTableColumn('descs','text','描述')
            ->addTableColumn('type','text','类型')
            ->addTableColumn('deletes','status','是否可删除',['可','不可'])
            ->addTableColumn('status','switch','状态',['开启','禁用'],'systems/change')
            ->addTableRightHeader('操作')
            ->addTableRight('edit','systems/show_edit','修改')
            ->addTableRight('del','systems/del','删除')
            ->addTablePage('systems/page')
            ->fetch();
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit',10);
        $data = System::where('id','>',0);
        if($tiaojian){
            $data = $data->where('name|en_name','like',"%$tiaojian%");
        }
        $data = $data->order('id desc')
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            $value['tupian'] = getFilePath($value['tupian']);
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }

    /**修改状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function change(Request $request){
        $id = $request->param('id');
        $data = System::where('id',$id)->findOrEmpty()->toArray();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(System::where('id',$id)->update(['status'=>$data['status']])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }
    /**添加数据
     * @return mixed
     */
    public function show_add(){
        return NewBuilder::make('form')
            ->addAction('systems/add')
            ->addInput('name','配置名称')
            ->addInput('en_name','配置英文名称(必填)')
            ->addInput('value','初始值')
            ->addTextarea('descs','描述')
            ->addSelect('type','配置类型',null,['文本','单图片'])
            ->addSwitch('status','状态',1)
            ->addSwitch('deletes','是否可删除',1)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(isset($data['deletes'])&&$data['deletes']=='on'){
            $data['deletes'] = 1;
        }else{
            $data['deletes'] = 0;
        }
        if(System::create($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data =System::where('id',$id)->findOrEmpty()->toArray();
//        echo '<pre>';print_r($data);exit;
        $newbuder = NewBuilder::make('form')
            ->addAction('systems/edit')
            ->addNone('id',$data['id'])
            ->addInput('name','配置名称',$data['name']);
        if($data['type']=='单图片'){
            $newbuder = $newbuder->addImg('tupian','图片',$data['tupian']);
        }elseif ($data['type']=='文本'){
            $newbuder = $newbuder->addInput('value','配置值',$data['value']);
        }
        $newbuder = $newbuder->addSwitch('status','状态',$data['status'])
            ->addSwitch('deletes','是否可删除',$data['deletes'])
            ->addTextarea('descs','描述',$data['descs'])
            ->addInput('en_name','英文名',$data['en_name'])
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
        return $newbuder;
    }

    /**编辑
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(isset($data['deletes'])&&$data['deletes']=='on'){
            $data['deletes'] = 1;
        }else{
            $data['deletes'] = 0;
        }
        if(System::where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**删除数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function del(Request $request){
        $id = $request->param('id');
        $system = System::where('id','in',$id)->select()->toArray();
        foreach ($system as $value){
            if($value['deletes']==0){
                return json_my_response('由不可删除数据');
            }
        }
        foreach ($system as $value){
            if($value['tupian']){
                UpFile::delFile($value['tupian']);
            }
        }
        if(System::where('id','in',$id)->delete()){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
}